﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Security.Principal;
using ICarPool.DTO;
using ICarPool.BLL;

namespace ICarPool.WebApp
{
    public class Global : System.Web.HttpApplication
    {

        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup

        }

        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown

        }

        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs

        }

        void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started

        }

        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.

        }

        void Application_AuthenticateRequest(Object sender, EventArgs e)
        {
            if (HttpContext.Current.User != null)//如果当前的http信息中存在用户信息
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)//如果当前用户的身份已经通过了验证
                {
                    /*
                    if (HttpContext.Current.User.Identity is FormsIdentity)
                    {
                        //如果当前用户身份是FormsIdentity类即窗体验证类,此类有个属性能够访问当前用户的验证票
                        FormsIdentity fi = (FormsIdentity)HttpContext.Current.User.Identity;//创建个FormsIdentity类,用他来访问当前用户的验证票
                        //获得用户的验证票
                        FormsAuthenticationTicket ticket = fi.Ticket;
                        //从验证票中获得用户数据也就是角色数据
                        string userData = ticket.UserData;
                        //把用户数据用,分解成角色数组
                        string[] roles = userData.Split(',');
                        //重写当前用户信息,就是把角色信息也加入到用户信息中
                        HttpContext.Current.User = new GenericPrincipal(fi, roles);
                    }
                    */

                    if (!(HttpContext.Current.User is CarPoolUser))
                    {

                  
                    }
                }
            }
        }

    }
}
